<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:26
 */
namespace app\shop\controller;
use alipay\Alipay;
use think\Db;
use wxpay\Wxpay;

class Center extends Base
{
    /**
     * 商家中心
     * @return mixed|\think\response\Json
     */
    public function centerPage(){
        if(request()->isPost ()){
            extract(input());
            $param['s_name'] = $name;
            $param['s_jianjie'] = $jianjie;
            $param['s_address'] = $s_address;
            $param['s_update_time'] = date('Y-m-d H:i:s');
            $param['s_sheng'] = $s_sheng;
            $param['s_shi'] = $s_shi;
            $param['s_qu'] = $s_qu;
            $param['s_lon'] = $s_lon;
            $param['s_lat'] = $s_lat;
            if($task_image!=''){
                $param['s_thumb'] = $task_image;
            }
            if($task_image1!=''){
                $param['s_image'] = $task_image1;
            }
            $res = Db::name('shops')->where('s_id',session('shop_id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('shops')
            ->where('s_id',session('shop_id'))
//            ->field (['s_status','s_id','s_name','s_address','s_thumb','s_image','s_jianjie','s_money','s_status','s_sheng','s_shi','s_qu','s_lat','s_lon'])
            ->find();
        $shi = array();
        $qu = array();
        $sheng = Db::table('up_area')
            ->where('level',1)
            ->field('district_id,district')
            ->select();
        if($data['s_sheng'] && $data['s_shi'] && $data['s_qu']){
            $shi = Db::table('up_area')
                ->where('pid',$data['s_sheng'])
                ->where('level',2)
                ->field('district_id,district')
                ->select();
            $qu = Db::table('up_area')
                ->where('pid',$data['s_shi'])
                ->where('level',3)
                ->field('district_id,district')
                ->select();
        }
        return $this->fetch ('center/center_page',['data'=>$data,'sheng'=>$sheng,'shi'=>$shi,'qu'=>$qu]);
    }

    /**
     * 明细
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function consumeDetail(){
        extract(input());
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $data = Db::name('shop_apply')
            ->where(['shop_id'=>session('shop_id')])
            ->order ('shop_apply_id desc')
            ->paginate (25);
        return $this->fetch ('center/consume_detail',['data'=>$data,'pages'=>$pages]);
    }
/**
     * 消息
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function newsDetail(){
        extract(input());
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $where = [
            'n_uors_id' =>session('shop_id')
        ];
        if(session('shop_auth') != 200){
            $where['n_title'] = ['<>','代理商审核通过'];
        }
        $data = Db::table('up_notice')
            ->where($where)
            ->where('n_type','in','1,3')
            ->order ('n_time desc')
            ->paginate (25);
        Db::table('up_notice')
            ->where($where)
            ->where('n_type','in','1,3')
            ->where('n_read',0)
            ->setField('n_read',1);
        return $this->fetch ('center/news_detail',['data'=>$data,'pages'=>$pages]);
    }

    /*****
     * @return mixed
     * 认证状态
     */
    public function approve(){
        $data = Db::name('shops')
            ->where(['s_id'=>session('shop_id')])
            ->find();
        if($data['s_status']==1){
            $data['status_name']='审核中';
        }elseif ($data['s_status']==2){
            $data['status_name']='通过';
        }elseif ($data['s_status']==3){
            $data['status_name']='拒绝';
        }

        return $this->fetch('center/approve',['data'=>$data]);
    }
    /**
     * 商家认证
     * @return mixed|\think\response\Json
     */
    public function approvePage(){
        if(request()->isPost ()){
            extract(input());
            $name = input('param.company');
            $zizhi = input('param.task_image');
            $three = input('param.task_image1');
            $code = input('param.code');
            $contact_name = input('param.username');
            $contact_phone = input('param.phone');
            $d = Db::name('shops')
                ->where('s_id',session('shop_id'))
                ->field (['s_id','s_code','s_code_time','s_code_status'])
                ->find();
            if($d['s_code_status'] == 1 || $d['s_code'] == ''){
                return json(['code'=>100,'msg'=>'请先发送验证码']);
            }
            $t = time() - $d['s_code_time'];
            if($t >= 600){
                return json(['code'=>100,'msg'=>'验证码失效']);
            }
            if($code != $d['s_code']){
                return json(['code'=>100,'msg'=>'验证码错误']);
            }
            $param = [
                's_name'=>$name,
                's_zizhi'=>$zizhi,
                's_three'=>$three,
                's_contact_name'=>$contact_name,
                's_contact_phone'=>$contact_phone,
                's_update_time'=>date('Y-m-d H:i:s'),
                's_tj_time'=>date('Y-m-d H:i:s'),
                's_code_status'=>1,
                's_status'=>1
            ];
            $param['s_sheng'] = $s_sheng;
            $param['s_shi'] = $s_shi;
            $param['s_qu'] = $s_qu;
            $param['s_lon'] = $s_lon;
            $param['s_lat'] = $s_lat;
            $res = Db::name('shops')->where('s_id',session('shop_id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'提交成功等待审核']);
            }else{
                return json(['code'=>100,'msg'=>'提交失败']);
            }
        }
        $data = Db::name('shops')
            ->where('s_id',session('shop_id'))
            ->field (['s_id','s_status','s_name','s_zizhi','s_three','s_contact_name','s_contact_phone','s_sheng','s_shi','s_qu','s_lat','s_lon'])
            ->find();
        $shi = array();
        $qu = array();
        $sheng = Db::table('up_area')
            ->where('level',1)
            ->field('district_id,district')
            ->select();
        if($data['s_sheng'] && $data['s_shi'] && $data['s_qu']){
            $shi = Db::table('up_area')
                ->where('pid',$data['s_sheng'])
                ->where('level',2)
                ->field('district_id,district')
                ->select();
            $qu = Db::table('up_area')
                ->where('pid',$data['s_shi'])
                ->where('level',3)
                ->field('district_id,district')
                ->select();
        }
        return $this->fetch ('center/approve_page',['data'=>$data,'sheng'=>$sheng,'shi'=>$shi,'qu'=>$qu]);
    }
    /*****
     * @return mixed
     * 活动认证状态
     */
    public function active_status(){
        $data = Db::name('shops')
            ->where(['s_id'=>session('shop_id')])
            ->find();
        if($data['s_active_status']==4){
            $data['status_name']='审核中';
        }elseif ($data['s_active_status']==2){
            $data['status_name']='通过';
        }elseif ($data['s_active_status']==3){
            $data['status_name']='拒绝';
        }

        return $this->fetch('center/active_status',['data'=>$data]);
    }
    /**
     * 商家活动任务认证
     * @return mixed|\think\response\Json
     */
    public function activeAsk(){
        if(request()->isPost ()){
            extract(input());
            $name = input('param.company');
            $contact_name = input('param.username');
            $contact_phone = input('param.phone');
            $param = [
                's_active_type'=>$name,
                's_active_name'=>$contact_name,
                's_active_phone'=>$contact_phone,
                's_update_time'=>date('Y-m-d H:i:s'),
                's_tj_time'=>date('Y-m-d H:i:s'),
                's_active_status'=>4
            ];
            $res = Db::name('shops')->where('s_id',session('shop_id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'提交成功等待审核']);
            }else{
                return json(['code'=>100,'msg'=>'提交失败']);
            }
        }
        $data = Db::name('shops')
            ->where('s_id',session('shop_id'))
//            ->field (['s_id','s_status','s_name','s_zizhi','s_three','s_contact_name','s_contact_phone','s_sheng','s_shi','s_qu','s_lat','s_lon'])
            ->find();
        if($data['s_status'] != 2){
            $this->redirect(url('shop/center/centerPage'));
        }else{
            return $this->fetch ('center/active_ask',['data'=>$data]);
        }
    }

    /**
     * 活动协议
     * @return mixed
     */
    public function active_xieyi(){
        $xieyi = Db::name('about_us')
            ->where('about_id',1)->value('task_agreement');
        return $this->fetch('center/active_xieyi',['xieyi'=>$xieyi]);
    }
    /**
     * 发送认证验证码
     * @return \think\response\Json
     */
    public function sendApproveCode(){
        extract(input());
        $code = GetRandCode(6);
        $res = YzxSms($code,$phone);
        if($res['code'] == 000000){
            Db::name('shops')->where('s_id',session('shop_id'))->update(['s_code'=>$code,'s_code_time'=>time(),'s_code_status'=>0]);
            return json(['code'=>200,'msg'=>'验证码发送成功，请注意查收']);
        }else{
            return json(['code'=>100,'msg'=>'验证码发送失败，请重试']);
        }

    }


    /**
     * 支付宝交易
     */
    public function alipay(){
//        $pay = new Alipay();
//        $result = $pay->pagePay ("35434534534543",'充值','100','','https://blog.wangkaiweb.com/index.php/category/php/','https://www.wangkaiweb.com/admin/login/resData');
//        return $result;
//        die;
        extract(input());
        $number = $this->getOnlyCode();
//        $money = 0.1;
        $param = [
            'order_number'=>$number,
            'order_shopid'=>session('shop_id'),
            'order_user_pay'=>$money,
            'order_coin'=>$money*10,
            'order_remark'=>'充值',
            'order_type'=>1,//支付宝
            'order_time'=>date('Y-m-d H:i:s')
        ];
        Db::name('order')->insert ($param);
        $pay = new Alipay();
        $result = $pay->pagePay ($number,'充值',$money,'',http_type ().'/shop/Center/centerPage',http_type ().'/shop/Callback/doOrder');
        return $result;
    }



    //微信支付
    public function wxPay(){
        extract(input());
        $number = $this->getOnlyCode();
//        $money = 0.1;
        $param = [
            'order_number'=>$number,
            'order_shopid'=>session('shop_id'),
            'order_user_pay'=>$money,
            'order_coin'=>$money*10,
            'order_remark'=>'充值',
            'order_type'=>2,//微信
            'order_time'=>date('Y-m-d H:i:s')
        ];
        $order_id = Db::name('order')->insertGetId ($param);
        $wx = new Wxpay();
        $res = $wx->wxPayApi("充值",$number,$money*100,http_type ().'/shop/Callback/fuckWx',"NATIVE",$order_id);
//        dump($res);die;
        return json(['msg'=>$res,'oid'=>$order_id]);
    }

    /**
     * 生成唯一订单号
     * @return string
     * @throws \think\Exception
     */
    public function getOnlyCode(){
        $code = date('YmdHis').GetRandCode(7);
        $repet = Db::name('order')->where('order_number',$code)->count();
        if(!$repet){
            return $code;
        }else{
            $this->getOnlyCode();
        }
    }
    
    /**
     * 充值页面
     * @return mixed
     */
    public function recharge(){
        $explain = Db::table('up_about_us')->where('about_id', 1)->value('recharge_explain');
        return $this->fetch('center/recharge', ['explain' => $explain]);
    }

    //删除明细
    public function del(){
        extract(input());
        $res = Db::name('shop_apply')->where('shop_apply_id',$id)->delete();
        if($res){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>100,'msg'=>'删除失败']);
        }
    }
    //删除消息
    public function delnews(){
        extract(input());
        $res = Db::name('notice')->where('n_id',$id)->delete();
        if($res){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>100,'msg'=>'删除失败']);
        }
    }

    //申请代理商
    public function unaudited(){
        $sheng = Db::table('up_area')
            ->where('level',1)
            ->field('district_id,district')
            ->select();
        $shi = Db::table('up_area')
            ->where('level',2)
            ->field('district_id,district')
            ->select();
        $this->assign('sheng',$sheng);
        $this->assign('shi',$shi);
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 申请代理商
     */
    public function addTask(){
        if(request ()->isPost()){
            $data['s_dl_name'] = input('param.username');
            $data['s_dl_phone'] = input('param.phone');
            $data['s_dl_sheng'] = input('param.sheng');
            $data['s_dl_shi'] = input('param.shi');
            $data['s_dl_qu'] = input('param.qu');
            $data['s_dl_level'] = input('param.level');
            $data['s_dl_content'] = input('param.intro');
            $data['s_dl_status'] = 2;
            $data['s_dl_time'] = date('Y-m-d H:i:s');
            if(input('param.level') == 1){
                if(!input('param.sheng')){
                    return json(['status' => 100, 'msg' => '省不能为空']);
                }
            }
            if(input('param.level') == 2){
                if(!input('param.sheng') || !input('param.shi')){
                    return json(['status' => 100, 'msg' => '省市不能为空']);
                }
            }
            if(input('param.level') == 3){
                if(!input('param.sheng') || !input('param.shi') || !input('param.qu')){
                    return json(['status' => 100, 'msg' => '省市区不能为空']);
                }
            }
            Db::table('up_shops')->where('s_id',session('shop_id'))->update($data);
            return json(['status' => 200, 'msg' => '成功']);
        }
    }
    public function detail(){
        $data = Db::name('shops')->alias('s')
            ->join('up_area a','s.s_dl_sheng=a.district_id','LEFT')
            ->join('up_area a2','s.s_dl_shi=a2.district_id','LEFT')
            ->join('up_area a3','s.s_dl_qu=a3.district_id','LEFT')
            ->where(['s_id'=>session('shop_id')])
            ->field('a.district sheng,a2.district shi,a3.district qu,s.*')
            ->find();
        $acc = Db::name('dl')
            ->where(['d_f_id'=>session('shop_id')])
            ->field('dl_account,password')
            ->find();
        $this->assign('data',$data);
        $this->assign('acc',$acc);
        return $this->fetch();
    }
}